package jcode.project.rest.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping(value = "/api/logout", method = RequestMethod.GET)
    @ResponseBody
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession(true).invalidate();
    }

    @RequestMapping(value = "/api/login", method = RequestMethod.GET)
    @ResponseBody
    public void login(String userid, HttpServletRequest request, HttpServletResponse response) {
        request.getSession(true).setAttribute("user", userid);
    }

    @RequestMapping(value = "/api/currentuser", method = RequestMethod.GET)
    @ResponseBody
    public String currentuser(String userid, HttpServletRequest request, HttpServletResponse response) {
        return (String)request.getSession(true).getAttribute("user");
    }

}

